package com.turing.usdtpay.mvp.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.squareup.picasso.MemoryPolicy;
import com.squareup.picasso.Picasso;
import com.turing.usdtpay.R;
import com.turing.usdtpay.base.BaseActivity;
import com.turing.usdtpay.entity.OrderInfoData;
import com.turing.usdtpay.http.ApiUrl;
import com.turing.usdtpay.manager.UserManager;
import com.turing.usdtpay.mvp.contract.OrderInfoContract;
import com.turing.usdtpay.mvp.dialog.ConfirmMoneyDialog;
import com.turing.usdtpay.mvp.dialog.QRDialog;
import com.turing.usdtpay.mvp.presenter.OrderInfoPresenter;
import com.turing.usdtpay.mvp.view.BaseTitleView;
import com.turing.usdtpay.utils.FormatUtils;
import com.turing.usdtpay.utils.QRCodeUtil;
import com.turing.usdtpay.utils.ToastUtils;

import java.io.IOException;
import java.io.Serializable;

/**
 * @作者：JAY
 * @创建时间：2019/10/20 23:48
 * @描述：
 **/
public class OrderReceiptActivity extends Activity implements View.OnClickListener ,OrderInfoContract.View {

    private BaseTitleView mTitle;
    private ImageView mQRCode;
    private OrderInfoContract.Present mPresent;
    private String mOrder,mUrl;
    private TextView mOrderTv,mState,mProportion,mUsdt,mTotal,mCode;
    private double mRate;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_receipt);
        initView();
        setPresenter(new OrderInfoPresenter(this));
        initData();
    }

    private void initData() {
        Intent intent = getIntent();
        OrderInfoData data = (OrderInfoData) intent.getSerializableExtra("data");
        if (data != null){
            mOrderTv.setText(data.getOrder());
            if (data.getStatus() == 0){
                mState.setText("待付款");
            }else if (data.getStatus() == 1){
                mState.setText("待收款");
            }else if (data.getStatus() == 2){
                mState.setText("已完成");
            }else if (data.getStatus() == 3){
                mState.setText("已取消");
            }
            mRate = (double) data.getMoney_change_usdt()/100;
            mProportion.setText(mRate+" CNY/USDT");
            mTotal.setText(FormatUtils.NoumberFromat2(((double)data.getMoney()/100))+" CNY");
            mUsdt.setText(FormatUtils.NoumberFromat8(((double)data.getMoney()/100)/mRate)+" USDT");
            if (data.getPay_type() == 2){
                mCode.setText("微信二维码");
                mUrl = ApiUrl.API_DOWNLOAD_WECHAT + UserManager.getInstance().getLoginDate().getTrader()+".jpg";
                Picasso.get().load(mUrl).memoryPolicy(MemoryPolicy.NO_CACHE).into(mQRCode);
            }else if (data.getPay_type() == 3){
                mCode.setText("支付宝二维码");
                mUrl = ApiUrl.API_DOWNLOAD_ALIPAY + UserManager.getInstance().getLoginDate().getTrader()+".jpg";
                Picasso.get().load(mUrl).memoryPolicy(MemoryPolicy.NO_CACHE).into(mQRCode);
            }
        }
    }

    private void initView() {
        mTitle = (BaseTitleView) findViewById(R.id.title);
        mQRCode = (ImageView) findViewById(R.id.qr_code_iv);
        mOrderTv = (TextView) findViewById(R.id.tv_order);
        mState = (TextView) findViewById(R.id.tv_state);
        mProportion = (TextView) findViewById(R.id.tv_proportion);
        mUsdt = (TextView) findViewById(R.id.tv_usdt);
        mTotal = (TextView) findViewById(R.id.tv_total);
        mCode = (TextView) findViewById(R.id.tv_code);
        mTitle.setLeftShow(true);
        mTitle.setLeftButtonListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mTitle.setTitle("订单详情");
    }

    @Override
    public void onClick(View v) {

    }

    @Override
    public void onSuccess(OrderInfoData data) {

    }

    @Override
    public void uploadSuccess(String url) {
        String s = ApiUrl.API_BASE_URL + url;
        Picasso.get().load(s).into(mQRCode);
    }

    @Override
    public void getSuccess() {

    }

    @Override
    public void paySuccess() {

    }

    @Override
    public void cancelSuccess() {

    }

    @Override
    public void onFail(String msg) {
        ToastUtils.showNormolToast(this,msg);
    }

    @Override
    public void onProgress(int progress) {

    }

    @Override
    public void setPresenter(OrderInfoContract.Present presenter) {
        mPresent = presenter;
    }
}
